﻿using MediatR;
using Microsoft.EntityFrameworkCore;
using System;
using W.Warehouse.Domain.DTO.ReportlossesDto;
using W.Warehouse.Domain.Reportlosses;
using W.Warehouse.Read.Api.Application.Command.ReportlossesCommand;
using W.Warehouse.Repository.Impl;
using W.Warehouse.Repository.Interface;

namespace W.Product.API.Appliaction.CommandHandler.ReportlossesCommandHandler
{
    public class ReportlossesUpdateCommandHandler : IRequestHandler<ReportlossesUpdateCommand, int>
    {
        private readonly IReportlossesRepository _reportlossesRepository;
        private readonly IReportProductRepository _reportProductRepository;
        public ReportlossesUpdateCommandHandler(IReportlossesRepository reportlossesRepository,
           IReportProductRepository reportProductRepository)
        {
            _reportlossesRepository = reportlossesRepository;
            _reportProductRepository = reportProductRepository;
        }
        /// <summary>
        /// 修改报损管理
        /// </summary>
        /// <param name="request"></param>
        /// <param name="cancellationToken"></param>
        /// <returns></returns>
        public async Task<int> Handle(ReportlossesUpdateCommand request, CancellationToken cancellationToken)
        {
            using (var tran = _reportlossesRepository.DB.Database.BeginTransaction(System.Data.IsolationLevel.Serializable))
            {
                try
                {
                    ReportlossesInfo info=new ReportlossesInfo();
                    info.ReportlossesId = request.ReportlossesId;
                    info.ReportlossesNo = request.ReportlossesNo;
                    info.ReportlossesType = request.ReportlossesType;
                    info.AssociateNumber = request.AssociateNumber;
                    info.SingleMaker = request.SingleMaker;
                    info.MakeTime = request.MakeTime;
                    info.MoveRemark = request.MoveRemark;
                    info.DocumentState = request.DocumentState;
                    info.ExamineReason = request.ExamineReason;
                    info.ExaminePeople = request.ExaminePeople;
                    info.ExamineTime = request.ExamineTime;
                    info.officeId = request.officeId;
                    info.userid = request.userid;
                    info.Wlocator = request.Wlocator;
                    info.WRepolocator=request.WRepolocator;
                    info.CreateName = request.CreateName;
                    info.CreateTime = request.CreateTime;
                    info.UpdateName = request.UpdateName;
                    info.UpdateTime = request.UpdateTime;
                    info.DeleteName = request.DeleteName;
                    info.DeleteTime = request.DeleteTime;
                    info.IsDelete = false;
                    await _reportlossesRepository.UpdateAsync(info);

                    foreach(var item in request.ReportProducts)
                    {
                        ReportProduct product = new ReportProduct();
                        product.ReportProductId=item.ReportProductId;
                        product.ProductName = item.ProductName;
                        product.ProductNo = item.ProductNo;
                        product.ProductSpec = item.ProductSpec;
                        product.Batch = item.Batch;
                        product.Price = item.Price;
                        product.ReportNum = item.ReportNum;
                        product.BinLocationName = item.BinLocationName;
                        product.officeId = item.officeId;
                        product.userid = item.userid;
                        product.ReportlossesId = item.ReportlossesId;
                        product.CreateName = item.CreateName;
                        product.CreateTime = item.CreateTime;
                        product.UpdateName = item.UpdateName;
                        product.UpdateTime = item.UpdateTime;
                        product.DeleteName = item.DeleteName;
                        product.DeleteTime = item.DeleteTime;
                        product.IsDelete = false;
                        await _reportProductRepository.UpdateAsync(product);
                    }
                    var code=await _reportlossesRepository.DB.SaveChangesAsync();
                    tran.Commit();
                    return code;
                }
                catch (Exception ex)
                {
                    tran.Rollback();
                    throw;
                }
            }
        }
    }
}
